﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Linq;


namespace ZX.WebApp.WebControls
{
    public class FlowXmlDefine
    {
        /// <summary>
        /// 加载系统
        /// </summary>
        /// <returns></returns>
        public List<AppSystem> ListSystem()
        {
            List<AppSystem> List = new List<AppSystem>();
            string Path = System.Web.Hosting.HostingEnvironment.MapPath("~/WorkFlow/WorkFlowObjects/BOSystemList.xml");
            XDocument xdoc = XDocument.Load(Path);
            var xmlTree = from c in xdoc.Descendants("System")
                          select c;
            if (xmlTree.Count() > 0)
            {
                foreach (var v in xmlTree)
                {
                    AppSystem sys = new AppSystem();
                    sys.Name = v.Attribute("Name").Value;
                    sys.Description = v.Attribute("Description").Value;
                    sys.ObjectFolder = v.Attribute("ObjectFolder").Value;
                    List.Add(sys);
                }
            }
            xdoc = null;
            return List;
        }
        /// <summary>
        /// 加载模块
        /// </summary>
        /// <param name="ObjectFolder"></param>
        /// <returns></returns>
        public List<AppModel> AppModel(string ObjectFolder)
        {
            List<AppModel> List = new List<AppModel>();
            string Path = System.Web.Hosting.HostingEnvironment.MapPath("~/WorkFlow/WorkFlowObjects/" + ObjectFolder + "/BOList.xml");
            XDocument xdoc = XDocument.Load(Path);
            var xmlTree = from c in xdoc.Descendants("ObjectList").Descendants<XElement>("Object")
                          select c;
            if (xmlTree.Count() > 0)
            {
                foreach (var v in xmlTree)
                {
                    AppModel model = new AppModel();
                    model.Name = v.Attribute("Name").Value;
                    model.Description = v.Attribute("Description").Value;
                    model.ObjectFolder = ObjectFolder;
                    List.Add(model);
                }
            }
            xdoc = null;
            return List;
        }
        /// <summary>
        /// 加载模块集合
        /// </summary>
        /// <param name="ObjectFolder"></param>
        /// <returns></returns>
        public List<AppModel> ListModel(List<string> ObjectFolder)
        {
            List<AppModel> List = new List<AppModel>();
            foreach (var item in ObjectFolder)
            {
                string Path = System.Web.Hosting.HostingEnvironment.MapPath("~/BusinessObjects/" + item + "/BOList.xml");
                XDocument xdoc = XDocument.Load(Path);
                var xmlTree = from c in xdoc.Descendants("ObjectList").Descendants<XElement>("Object")
                              select c;
                if (xmlTree.Count() > 0)
                {
                    foreach (var v in xmlTree)
                    {
                        AppModel model = new AppModel();
                        model.Name = v.Attribute("Name").Value;
                        model.Description = v.Attribute("Description").Value;
                        model.ObjectFolder = item;
                        List.Add(model);
                    }
                }
                xdoc = null;
            }
            return List;
        }


        public AppFunc ListSystemFunc(string ObjectFolder, string strFolderName, string MsgLinkUrl)
        {
            try
            {
                
                string Path = System.Web.Hosting.HostingEnvironment.MapPath("~/WorkFlow/WorkFlowObjects/" + ObjectFolder + "/" + strFolderName + ".xml");
                XDocument xdoc = XDocument.Load(Path);
                var xmlFunction = from p in xdoc.Descendants("Function")
                                 select p;
                AppFunc sys = new AppFunc();
                if (xmlFunction.Count() > 0)
                {
                    foreach (var xml in xmlFunction)
                    {
                        sys.PublicMethod = xml.Attribute("PublicMethod").Value; //方法名称
                        sys.PublicCalss = xml.Attribute("PublicCalss").Value; //接口类
                        sys.ServiceWsdlUri = xml.Attribute("ServiceWsdlUri").Value; //服务地址
                        sys.ParamterCalss = xml.Attribute("ParamterCalss").Value; //参数实体
                        sys.WorkFlowId = xml.Attribute("WorkFlowId").Value; //流程ID
                        sys.ObjectFolder = xml.Attribute("ObjectFolder").Value;
                        List<ParameterEntity> ListParam = new List<ParameterEntity>();
                        var paramter = xml.Descendants("PageParameter").Descendants<XElement>("Para");
                        if (paramter.Count() > 0)
                        {
                            foreach (var v in paramter)
                            {
                                ParameterEntity para = new ParameterEntity();
                                para.Name = v.Attribute("Name").Value.CvtString(); //
                                para.Description = v.Attribute("Description").Value.CvtString();//
                                para.Value = v.Attribute("Value").Value.CvtString();
                                para.Sataus = v.Attribute("Sataus").Value.CvtString();
                                ListParam.Add(para);
                            }
                            sys.Parameter = ListParam;
                        }
                    }
                }
                return sys;
            }
            catch (Exception ex)
            {
            }
            return null;
        }
        public List<TableColumn> ListTableColumn(string ObjectFolder, string strFileName)
        {
            try
            {
                List<TableColumn> list = new List<TableColumn>();
                string Path = System.Web.Hosting.HostingEnvironment.MapPath("~/BusinessObjects/" + ObjectFolder + "/" + strFileName + ".xml");
                XDocument xdoc = XDocument.Load(Path);
                var xmlTree = from c in xdoc.Descendants("Object").Descendants<XElement>("Attribute")
                              select c;
                if (xmlTree.Count() > 0)
                {
                    foreach (var v in xmlTree)
                    {
                        TableColumn sys = new TableColumn();
                        sys.Language = v.Attribute("Description").Value;
                        sys.DataType = v.Attribute("DataType").Value;
                        sys.DataValue = v.Attribute("DataValue").Value;
                        sys.Description = v.Attribute("Description").Value;
                        sys.FieldName = v.Attribute("Name").Value;
                        list.Add(sys);
                    }
                }
                xdoc = null;
                return list;
            }
            catch (Exception ex)
            {
                //ErrorLog.WriteErrorMessage(ex.Message + "|" + DateTime.Now.ToString() + "|" + ex.Source + "|" + "UserDefine.svc方法GetSystemFuncList");
            }
            return null;
        }
        //public Dictionary<List<AppFunc>, List<TableColumn>> ListFuncTableColumn(string ObjectFolder, string strFileName, ref string MsgLinkUrl)
        //{
        //    Dictionary<List<AppFunc>, List<TableColumn>> FuncTableColumn = new Dictionary<List<AppFunc>, List<TableColumn>>();
        //    try
        //    {
        //        List<AppFunc> list = new List<AppFunc>();
        //        string Path = System.Web.Hosting.HostingEnvironment.MapPath("~/BusinessObjects/" + ObjectFolder + "/" + strFileName + ".xml");
        //        XDocument xdoc = XDocument.Load(Path);
        //        try
        //        {
        //            string strXml = xdoc.Document.ToString();
        //            int Start = strXml.IndexOf("<MsgOpen>") + "<MsgOpen>".Length;
        //            int End = strXml.IndexOf("</MsgOpen>");
        //            MsgLinkUrl = strXml.Substring(Start, End - Start);
        //        }
        //        catch { }
        //        var xmlTree = from c in xdoc.Descendants("Function")
        //                      select c;
        //        if (xmlTree.Count() > 0)
        //        {
        //            foreach (var v in xmlTree)
        //            {
        //                AppFunc sys = new AppFunc();
        //                sys.Language = v.Attribute("Description").Value;
        //                sys.FuncName = v.Attribute("FuncName").Value;

        //                // sys.Parameter = v.Attribute("Parameter").Value;
        //                sys.Address = v.Attribute("Address").Value;
        //                sys.Binding = v.Attribute("Binding").Value;
        //                sys.SplitChar = v.Attribute("SplitChar").Value;
        //                List<Parameter> ListParam = new List<Parameter>();
        //                var param = v.Descendants("ParaStr").Descendants<XElement>("Para");
        //                if (param.Count() > 0)
        //                {
        //                    foreach (var p in param)
        //                    {
        //                        Parameter para = new Parameter();
        //                        para.Description = p.Attribute("Description").Value.CvtString();
        //                        para.Name = p.Attribute("Name").Value.CvtString();
        //                        para.Value = p.Attribute("Value").Value.CvtString();
        //                        para.TableName = p.Attribute("TableName").Value.CvtString();
        //                        ListParam.Add(para);
        //                    }
        //                }
        //                sys.Parameter = ListParam;
        //                list.Add(sys);
        //            }
        //        }
        //        List<TableColumn> lists = new List<TableColumn>();
        //        var xmlTrees = from c in xdoc.Descendants("Object").Descendants<XElement>("Attribute")
        //                       select c;
        //        if (xmlTrees.Count() > 0)
        //        {
        //            foreach (var v in xmlTrees)
        //            {
        //                TableColumn sys = new TableColumn();
        //                sys.Language = v.Attribute("Description").Value;
        //                sys.DataType = v.Attribute("DataType").Value;
        //                sys.DataValue = v.Attribute("DataValue").Value;
        //                sys.Description = v.Attribute("Description").Value;
        //                sys.FieldName = v.Attribute("Name").Value;
        //                lists.Add(sys);
        //            }
        //        }
        //        xdoc = null;
        //        FuncTableColumn.Add(list, lists);
        //        return FuncTableColumn;
        //    }
        //    catch (Exception ex)
        //    {
        //        //ErrorLog.WriteErrorMessage(ex.Message + "|" + DateTime.Now.ToString() + "|" + ex.Source + "|" + "UserDefine.svc方法GetSystemFuncList");
        //    }
        //    return null;
        //}

       

    }
}